<?php

/**
 * 课表转换，熊义34512664@qq.com
 */

namespace plugins\fuck\lib;

//use think\Db;
use think\facade\Db;
use think\Validate;

abstract class Kbzf
{

    /**
     * 基础数据保存
     */
    public function save_jcsj($d)
    {
        $ret = ['code' => 0, 'msg' => '', 'data' => ''];
        Db::startTrans();
        try {
            //先判断是否已经有这个班级了
            $w['xxmc'] = $d['xxmc'];
            $w['xnxq'] = $d['xnxq'];
            $w['njmc'] = $d['nj'];
            $w['bjmc'] = $d['bjmc'];
            $id = Db::name('kb_base_bj')->where($w)->value('id');

            $data = [
                'type' => 1,
                'xxmc' => $d['xxmc'],
                'xnxq' => $d['xnxq'],
                'njmc' => $d['nj'],
                'bjbh' => $d['bjbh'],
                'bjmc' => $d['bjmc'],
                'csbh' => $d['csbh'],
                'csmc' => $d['csmc']
            ];
            if (empty($id)) {
                $data['create_time'] = time();
                $id = Db::name('kb_base_bj')->insertGetId($data, true); //直接替换->insertGetId($data);
            } else {
                $data['update_time'] = time();
                $id = Db::name('kb_base_bj')->where('id', $id)->update($data); //直接替换->insertGetId($data);
            }

            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            var_dump($e);
            $ret['code'] = 0;
            $ret['msg'] = $e->getMessage();
            $ret['data'] = $d;
            return $ret;
        }
        $ret['code'] = 1;
        $ret['msg'] = '成功保存';
        $ret['data'] = $d;
        return $ret;
    }

    /**
     * 将老师表里的老师写入数课表基础数据表
     */
    public function save_ls($d)
    {
        $ret = ['code' => 0, 'msg' => '', 'data' => ''];
        Db::startTrans();
        try {
            //先判断有没有这个班级
            $w['xxmc'] = $d['xxmc'];
            $w['xnxq'] = $d['xnxq'];
            $w['njmc'] = $d['nj'];
            $w['bjmc'] = $d['bjmc'];
            $bj_id = Db::name('kb_base_bj')->where($w)->value('id');
            if (empty($bj_id)) {
                return ['code' => 0, 'msg' => '没有这个班级', 'data' => $d];
            }
            //写入老师资料表
            $w = [];
            $w['xxmc'] = $d['xxmc'];
            $w['xnxq'] = $d['xnxq'];
            $w['njmc'] = $d['nj'];
            $w['lsxm'] = $d['lsxm'];
            $id = Db::name('kb_base_ls')->where($w)->value('id');
            $data = ['type' => 1, 'xxmc' => $d['xxmc'],  'lsxm' => $d['lsxm'], 'xnxq' => $d['xnxq'], 'njmc' => $d['nj']];
            if (empty($id)) {
                $data['create_time'] = time();
                $id = Db::name('kb_base_ls')->insertGetId($data, true); //直接替换->insertGetId($data);
            } else {
                $data['update_time'] = time();
                $id = Db::name('kb_base_ls')->where('id', $id)->update($data); //直接替换->insertGetId($data);
            }

            $w = [];
            $w['xxmc'] = $d['xxmc'];
            $w['xnxq'] = $d['xnxq'];
            $w['njmc'] = $d['nj'];
            $w['bjmc'] = $d['bjmc'];
            $w['kcmc'] = $d['kcmc'];
            $w['lsxm'] = $d['lsxm'];
            $id = Db::name('kb_kc_ls')->where($w)->value('id');
            $zc = $this->zf_zc1($d['zc']);
            $data = [
                'xxmc' => $d['xxmc'],
                'xnxq' => $d['xnxq'],
                'njmc' => $d['nj'],
                'bjmc' => $d['bjmc'],
                'kcmc' => $d['kcmc'],
                'lsxm' => $d['lsxm']
            ];
            if (empty($id)) {
                $data['create_time'] = time();
                $d_id = Db::name('kb_kc_ls')->insertGetId($data);
            } else {
                $data['update_time'] = time();
                $d_id = Db::name('kb_kc_ls')->where('id', $id)->update($data);
            }

            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            var_dump($e);
            $ret['code'] = 0;
            $ret['msg'] = $e->getMessage();
            $ret['data'] = $d;
            return $ret;
        }
        $ret['code'] = 1;
        $ret['msg'] = '成功保存';
        $ret['data'] = $d;
        return $ret;
    }

    /**
     * 转换并写入课表，单个单元格分析
     */
    public function save_kb($d)
    {
        $ret = ['code' => 0, 'msg' => '', 'data' => $d];
        Db::startTrans();
        try {
            //取得各字段id号
            if (empty($d)) return ['code' => 0, 'msg' => '空数据', 'data' => $d];    //空行不处理
            if (empty($d['kcmc'])) return ['code' => 0, 'msg' => '课程名称为空', 'data' => $d]; //课程名称为空则不处理

            var_dump("一行课表上青天：");
            var_dump($d);

            $xxmc = $d['xxmc'];
            $xnxq = $d['xnxq'];
            $njmc = $d['nj'];

            $njmc = $d['nj'];             //年级名称
            $xq = $d['xq'];             //星期
            $jc = $d['jc'];             //节次
            $bjmc = $d['bjmc'];         //班级名称

            //分析“自习-语/英”这样的以横杠和反斜杠分割的课程
            $kc_ar = $this->zf_kc($d['kcmc']);         //课程名称
            var_dump("分解后的课程：");
            var_dump($kc_ar);

            $kcmc = null;       //课程名称
            $kcbh = null;       //课程编号
            $lsxm = null;       //老师姓名
            $lsbh = null;       //老师编号
            $zc = null;

            $kc_count = count($kc_ar);
            foreach ($kc_ar as $k => $v) {

                //判断是否分单双周,以/号分隔的课程使用单双周编排，/前的单周，/后的双周
                if ($kc_count <= 1) {
                    $zc_ar = $this->zf_zc1($d['zc']);
                    $zc = implode(",", $zc_ar);             //取得周次
                    var_dump('分解后的zc：', $zc);
                } else {
                    $zc_ar = $this->zf_zc($d['zc']);
                    $v_zc = "";
                    if ($k == 0) $v_zc = $zc_ar['dz'];      //单周
                    if ($k == 1) $v_zc = $zc_ar['sz'];      //双周
                    $zc = implode(",", $v_zc);             //取得周次
                    var_dump('编排单双周的zc：', $zc);
                }
                //如果课程是  自习  单独分开
                if ($v == '自习') {
                    $kcmc = $v;             //课程名称
                    $kcbh = 'zx65535';       //课程编号
                    $lsxm = '无';           //老师姓名
                    $lsbh = 'ls65535';      //老师编号
                } else {
                    //查找如 自习-语,语文,自习-数,数  这样的课程名称
                    $kc_list = Db::name('kb_base_kc')
                        ->whereRaw(
                            'xxmc=:xxmc and xnxq=:xnxq and njmc=:njmc and (kcmc=:kcmc or kcjc=:kcjc)',
                            ['xxmc' => $xxmc, 'xnxq' => $xnxq, 'njmc' => $njmc, 'kcmc' => $v, 'kcjc' => $v]
                        )->find();
                    if (empty($kc_list)) {
                        throw new \Exception("没有名称是${v}的课程");
                    }
                    var_dump("课程数据：");
                    var_dump($kc_list['kcmc'], $kc_list['id']);
                    $kcmc = $kc_list['kcmc'];     //课程名称
                    $kcbh = $kc_list['id'];       //课程编号

                    //分析 课程名称里的-符，用于查找课程对应老师，例如“自习-语文”
                    $ok = strpos($kcmc, "-");
                    $w = [];
                    if ($ok) {
                        $ar = explode("-", $kcmc);     //分解自习-语文，去掉自习
                        var_dump('再次分析课程：', $ar[1]);
                        //查找教这门课的老师
                        $w['kcmc'] = $ar[1];
                    } else {
                        //查找教这门课的老师
                        $w['kcmc'] = $kcmc;
                    }
                    $w['xxmc'] = $xxmc;
                    $w['xnxq'] = $xnxq;
                    $w['njmc'] = $njmc;
                    $w['bjmc'] = $bjmc;
                    //查找教这门课的老师
                    $lsxm = Db::name('kb_kc_ls')->where($w)->value('lsxm');
                    if (empty($lsxm)) {
                        throw new \Exception("从数据库里不能查找到${ar[1]}的老师");
                    }
                    var_dump('lsxm:');
                    var_dump($lsxm);

                    //查找此老师的编号
                    $w = [];
                    $w['xxmc'] = $xxmc;
                    $w['xnxq'] = $xnxq;
                    $w['njmc'] = $njmc;
                    $w['lsxm'] = $lsxm;
                    $lsbh = Db::name('kb_base_ls')->where($w)->value('id');
                    if (empty($lsbh)) {
                        throw new \Exception("从数据库里不能查找到${lsxm}的老师编号");
                    }
                    var_dump('lsbh:');
                    var_dump($lsbh);
                }

                //查找班级和场所
                $w = [];
                $w['xxmc'] = $xxmc;
                $w['xnxq'] = $xnxq;
                //$w['njmc'] = $njmc;
                $w['bjmc'] = $bjmc;
                $bj_ar = Db::name('kb_base_bj')->where($w)->find();
                if (empty($bj_ar)) {
                    throw new \Exception("从数据库里不能查找到名称是${bjmc}的班级");
                }
                var_dump('班级和场所数据:');
                var_dump($bj_ar['bjmc'], $bj_ar['bjbh'], $bj_ar['csmc'], $bj_ar['csbh']);
                $bjmc = $bj_ar['bjmc'];
                $bjbh = $bj_ar['bjbh'];
                $csmc = $bj_ar['csmc'];
                $csbh = $bj_ar['csbh'];

                //查找有没有这个课表
                $w = [];
                $w['xxmc'] = $xxmc;
                $w['xnxq'] = $xnxq;
                $w['njmc'] = $njmc;
                $w['bjmc'] = $bjmc;
                $w['xq'] = $xq;
                $w['jc'] = $jc;
                $w['zc'] = $zc;
                $id = Db::name('kb_bj_kb')->where($w)->value('id');
                //$fields = "xxmc,xnxq,zc,xq,jc,njmc,bjmc,bjbh,kcmc,kcbh,lsxm,lsbh,csmc,csbh";
                $data = [
                    'xxmc' => $xxmc,
                    'xnxq' => $xnxq,
                    'njmc' => $njmc,
                    'zc' => $zc,
                    'xq' => $xq,
                    'jc' => $jc,
                    'bjmc' => $bjmc,
                    'bjbh' => $bjbh,
                    'kcmc' => $kcmc,
                    'kcbh' => $kcbh,
                    'lsxm' => $lsxm,
                    'lsbh' => $lsbh,
                    'csmc' => $csmc,
                    'csbh' => $csbh
                ];
                if (empty($id)) {
                    $data['create_time'] = time();
                    Db::name('kb_bj_kb')
                        //->field($fields)
                        ->insertGetId($data, true);
                } else {
                    $data['update_time'] = time();
                    $id = Db::name('kb_bj_kb')
                        //->field($fields)
                        ->where('id', $id)->update($data);
                }
            }

            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            $ret['code'] = 0;
            $ret['msg'] = $e->getMessage();
            $ret['data'] = $d;
            return $ret;
        }

        $ret['code'] = 1;
        $ret['msg'] = '成功保存';
        $ret['data'] = $d;
        return $ret;
    }

    /**
     * 分析“自习-语/英”这样的以横杠和反斜杠分割的课程 ，自习-语/英分解成：自习语和自习英
     */
    public function zf_kc($kc)
    {
        $v_first = null;
        $v_kc = [];

        $ok = strpos($kc, "-");
        //如果有-符，则分析自习
        if ($ok) {
            $ar = explode("-", $kc);
            if ($ar[0] == '自习') $v_first = '自习-';
            foreach ($ar as $k => $v) {
                if ($v == '自习') {
                    continue;
                }
                $ok = strpos($v, "/");
                if ($ok) {
                    $a = explode("/", $v);
                    foreach ($a as $k1 => $v1) {
                        $v_kc[] = $v_first . $v1;
                    }
                } else {
                    if ($v == '自习') {
                        continue;
                    }
                    $v_kc[] = $v_first . $v;
                }
            }
            return $v_kc;
        }

        $ok = strpos($kc, "/");
        //如果有/符，则只分解课名
        if ($ok) {
            $ar = explode("/", $kc);
            foreach ($ar as $k => $v) {
                $v_kc[] = $v;
            }
            return $v_kc;
        }

        //没有/也没有-符，则直接返回课程名称
        $v_kc[] = $kc;
        return $v_kc;
    }

    /**
     * 分析1-23,24,26这样的以逗号分割的周次，不分单双周 
     */
    public function zf_zc1($zc)
    {
        $v_zc = [];
        $ar = explode(",", $zc);
        foreach ($ar as $k => $v) {
            $ok = strpos($v, "-");
            if ($ok) {
                $a = explode("-", $v);
                $c1 = intval($a[0]);
                $c2 = intval($a[1]);
                for ($i = $c1; $i <= $c2; $i++) {
                    $v_zc[] = $i;
                }
            } else {
                if (is_numeric($v)) $v_zc[] = intval($v);
            }
        }
        return $v_zc;
    }

    /**
     * 分析1-23,24,26这样的以逗号分割的周次，分单双周返回
     */
    public function zf_zc($zc)
    {
        $v_zc = [];
        $ar = explode(",", $zc);
        foreach ($ar as $k => $v) {
            $ok = strpos($v, "-");
            if ($ok) {
                $a = explode("-", $v);
                $c1 = intval($a[0]);
                $c2 = intval($a[1]);
                for ($i = $c1; $i <= $c2; $i++) {
                    $v_zc[] = $i;
                }
            } else {
                if (is_numeric($v)) $v_zc[] = intval($v);
            }
        }

        //分单双周
        $zc_dz = [];
        $zc_sz = [];
        foreach ($v_zc as $k => $v) {
            if ($v % 2 == 1) {
                //奇数
                $zc_dz[] = $v;
            } else if ($v % 2 == 0) {
                //偶数
                $zc_sz[] = $v;
            }
        }
        return [
            //"zc" => $v_zc,
            "dz" => $zc_dz,
            "sz" => $zc_sz,
        ];
    }
}
